package org.lex.jdbc.util;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class SQLTypes {
	private static final SQLTypes INSTANCE = new SQLTypes();

	public static SQLTypes getInstance() {
		return INSTANCE;
	}

	public static String getName(int type) {
		return getInstance().typeNames.get(type);
	}

	public static int[] getTypes() {
		int[] o = getInstance().types;
		return Arrays.copyOf(o, o.length);
	}

	private int[] types = null;
	private Map<Integer, String> typeNames = null;

	private SQLTypes() {
		Field[] fields = java.sql.Types.class.getFields();
		int count = fields.length;
		types = new int[count];
		typeNames = new HashMap<Integer, String>(count);
		for (int i = 0; i < count; i++) {
			Field f = fields[i];
			try {
				int type = f.getInt(null);
				String name = f.getName();
				types[i] = type;
				typeNames.put(type, name);
				System.out.println(type + "\t --> " + name);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		System.out.println(count + " SQL TYPEs loaded");
	}
}
